package v1

import (
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/model/req_resp"
	v1 "RoccBlog/repository/v1"
	"RoccBlog/service"
	"github.com/gin-gonic/gin"
)

// TaskProgressService 任务进度业务层
type TaskProgressService struct {
	service.BaseService
	//接口封装业务层CURD
	service.BaseOperationService
	//依赖的持久层
	Repository v1.TaskProgressRepository
}

// 将TaskProgressService注册到全局
func init() {
	global.RegisterService(&TaskProgressService{
		Repository: v1.TaskProgressRepository{},
	})
}

// Add 添加任务进度
func (s *TaskProgressService) Add(ctx *gin.Context, data model.TaskProgress) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	//设置添加记录人未当前登录用户
	data.CreateBy = userId

	return s.Repository.Create(data)
}

// Remove 删除任务进度
func (s *TaskProgressService) Remove(ctx *gin.Context, id int) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	return s.Repository.Delete(id, userId)
}

// Modify 修改任务进度
func (s *TaskProgressService) Modify(ctx *gin.Context, data model.TaskProgress) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	return s.Repository.Update(data, userId)
}

// FindById 查找ById任务进度
func (s *TaskProgressService) FindById(ctx *gin.Context, id int) (model.TaskProgress, error) {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return model.TaskProgress{}, err
	}
	return s.Repository.SelectById(id, userId)
}

// Find 查找列表任务进度
func (s *TaskProgressService) Find(ctx *gin.Context, params req_resp.TaskProgressReq) (any, error) {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return model.TaskProgress{}, err
	}
	size, num := s.GetPage(ctx)
	return s.Repository.Select(params, num, size, userId)
}
